from app.core import MockResponseType, exceptions, error_code
from functools import wraps
from typing import Callable


def mock_response(get_response, mock_type: MockResponseType, is_simulating: bool = False):
    def mock_response_func(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            if not is_simulating:
                return await func(*args, **kwargs)
            else:
                try:
                    result = await get_response(_mock_type=mock_type)
                    return result
                except Exception as e:
                    raise exceptions.MockDataErrorException(
                        error_code=error_code.ErrorCode.MOCK_DATA_ERROR,
                        detail=str(e)
                    )

        return wrapper

    return mock_response_func

def return_response(detail=""):
    def return_response_func(func: Callable) -> Callable:
        @wraps(func)
        async def wrapper(*args, **kwargs):
            mock_type = kwargs.get('_mock_type')
            if mock_type == MockResponseType.MockData_200 or mock_type == MockResponseType.MockData_201:
                try:
                    return await func(*args, **kwargs)
                except Exception as e:
                    raise exceptions.MockDataErrorException(
                        error_code=error_code.ErrorCode.MOCK_DATA_ERROR,
                        detail=str(e)
                    )
            else:
                raise exceptions.MockDataErrorException(
                    error_code=error_code.ErrorCode.MOCK_DATA_ERROR,
                    detail=detail
                )
        return wrapper
    return return_response_func